//
//  OImManager.m
//  OFootball
//
//  Created by Alan on 15/9/23.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "OImManager.h"


@implementation OImManager

static OImManager* _manager = nil;

+(OImManager*) shareManager
{
    if (_manager) {
        return _manager;
    }
    _manager = [[OImManager alloc] init];
    return _manager;
}

-(void) initManager:(NSDictionary *)options
{
    UIApplication* application = [UIApplication sharedApplication];
    AppDelegate* appDelegate = (AppDelegate*)[application delegate];
    
    [appDelegate easemobApplication:application didFinishLaunchingWithOptions:options
     ];
}

//我们自己的进入服务器完成后，最后调用，且后面不能再有其他掉用
-(void) asynLoginWithUserName:(NSString*)userName password:(NSString*)password
{
    //TODO:self showHudInView:self.view hint:NSLocalizedString(@"login.ongoing", @"Is Login...")];
    //异步登陆账号
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:userName
                                                        password:password
                                                      completion:
     ^(NSDictionary *loginInfo, EMError *error) {
         if (loginInfo && !error) {
             //[self hideHud];
             //设置是否自动登录
             //TODO:[[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
             
             // 旧数据转换 (如果您的sdk是由2.1.2版本升级过来的，需要家这句话)
             [[EaseMob sharedInstance].chatManager importDataToNewDatabase];
             //获取数据库中数据
             [[EaseMob sharedInstance].chatManager loadDataFromDatabase];
             
             //获取群组列表
             //TODO:[[EaseMob sharedInstance].chatManager asyncFetchMyGroupsList];
             
             //发送自动登陆状态通知
             [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES];
             
         }
         else
         {
             switch (error.errorCode)
             {
                 case EMErrorNotFound:
                     TTAlertNoTitle(error.description);
                     break;
                 case EMErrorNetworkNotConnected:
                     TTAlertNoTitle(NSLocalizedString(@"error.connectNetworkFail", @"No network connection!"));
                     break;
                 case EMErrorServerNotReachable:
                     TTAlertNoTitle(NSLocalizedString(@"error.connectServerFail", @"Connect to the server failed!"));
                     break;
                 case EMErrorServerAuthenticationFailure:
                     TTAlertNoTitle(error.description);
                     break;
                 case EMErrorServerTimeout:
                     TTAlertNoTitle(NSLocalizedString(@"error.connectServerTimeout", @"Connect to the server timed out!"));
                     break;
                 default:
                     TTAlertNoTitle(NSLocalizedString(@"login.fail", @"Login failure"));
                     break;
             }
         }
     } onQueue:nil];
}



@end
